<?php
namespace App\Http\Controllers\Api\v1\User;
use App\Http\Controllers\Api\BaseController;
use DB;
// 用户中心
class IndexController extends BaseController
{
    private $user;

    public function __construct()
    {
        parent::filter_xss();
        if(!defined('IS_ACCESS_TOKEN_CHECKED'))
        {
            parent::__construct();// 接口调用验证
        }
        $this->user = model('User');

    }
    /**
     * 后台首页
     * @return [type] [description]
     */
    public function index(){

        return response()->json(['code'=>200,'msg'=>'user-index-index']);
    }
    /**
     * 新增账号时  只记录账户名密码
     * [add description]
     */
    public function add()
    {
        $username = htmlspecialchars(input('username'));// 账户
        $password = htmlspecialchars(input('password'));// 密码
        $data = [
            'username'=>$username,
            'password'=>$password,
        ];
        $result = $this->user->validate(
            [
                'username'=>'require|unique:user|max:25',
                'password'=>'require'
            ],
            [
                'username.require'=>'账号必填！',
                'username.unique'=>'账号已存在！',
                'username.max'=>'账号长度不符！',
                'password.require'=>'密码不能为空！',
            ]
        )->save($data);
        if($result === false) return response()->json(['code'=>10014,'msg'=>$this->user->getError()]);

        $data['id']=  intval($this->user->id);

        return response()->json(['code'=>200,'msg'=>'ok','data'=>$data]);
    }

    public function update()
    {
        $id = intval(input('id'));// 用户ID
        $password = htmlspecialchars(input('password'));// 密码
        $nickname = htmlspecialchars(input('nickname'));// 昵称
        $sex = intval(input('sex'));// 性别
        $avatar = htmlspecialchars(input('avatar'));// 头像
        $status = htmlspecialchars(input('status'));// 状态
        $email = htmlspecialchars(input('email'));// 邮箱
        $tel = htmlspecialchars(input('tel'));// 手机号+86

        if(!$id) return response()->json(['code'=>10014,'msg'=>'账号不存在！']);

        if($nickname) $data['nickname'] = $nickname;
        $data = [
            'id'=>$id,
            'password'=>$password,
            'nickname'=>$nickname ? $nickname : null,
            'sex'=>$sex ? $sex : null,
            'avatar'=>$avatar ? $avatar : null,
            'status'=>$status ? $status : null,
            'email'=>$email ? $email : null,
            'tel'=>$tel ? $tel : null,
        ];
        $result = $this->user->validate(
            [
                'id'=>'require',
                'password'=>'require'
            ],
            [
                'id.require'=>'账号不存在！',
                'password.require'=>'密码不能为空！',
            ]
        )->save($data);

        if($result === false) return response()->json(['code'=>10014,'msg'=>$this->user->getError()]);

        $data['id']=  intval($this->user->id);
        return response()->json(['code'=>200,'msg'=>'ok','data'=>$data]);

    }
    /**
     * 获取用户列表
     * @return [type] [description]
     */
    public function getList()
    {
        $data = $this->user->select();

        return response()->json($data);
    }


}
